using System;
using System.Data;
using System.Web.Security;
using umbraco.BusinessLogic;
using umbraco.BasePages;
using umbraco.interfaces;
using Commerce.Common;

namespace CommerceForUmbraco.adminSection.tasks
{
    public class CategoryTasks : umbraco.interfaces.ITaskReturnUrl
    {
        private string _alias;
        private int _categoryId;
        private int _parentID;
        private int _typeID;
        private int _userID;

        public int UserId
        {
            set { _userID = value; }
        }

        public int TypeID
        {
            set { _typeID = value; }
            get { return _typeID; }
        }

        public int ParentID
        {
            set { _parentID = value; }
            get { return _parentID; }
        }

        public int CategoryId
        {
            set { _categoryId = value; }
            get { return _categoryId; }
        }

        public string Alias
        {
            get
            {
                return _alias;
            }
            set
            {
                _alias = value;
            }
        }

        public bool Save()
        {
            Category thisCat = new Category();
            thisCat.CategoryName = _alias;
            thisCat.Save();

            int id = thisCat.CategoryID;
            m_returnUrl = string.Format("commerce/editCategory.aspx?c={0}", id);
            return true;
        }

        public bool Delete()
        {
            
            return false;
        }

        public CategoryTasks()
        {

        }

        #region ITaskReturnUrl Members
        private string m_returnUrl = "";
        public string ReturnUrl
        {
            get { return m_returnUrl; }
        }

        #endregion

    }

    public class ProductTasks : umbraco.interfaces.ITaskReturnUrl
    {
        private string _alias;
        private int _parentID;
        private int _typeID;
        private int _userID;

        public int UserId
        {
            set { _userID = value; }
        }

        public int TypeID
        {
            set { _typeID = value; }
            get { return _typeID; }
        }

        public int ParentID
        {
            set { _parentID = value; }
            get { return _parentID; }
        }
	
        public string Alias
        {
            get
            {
                return _alias;
            }
            set
            {
                _alias = value;
            }
        }

        public bool Save()
        {
           int id = InsertProduct();
            m_returnUrl = string.Format("commerce/editProduct.aspx?p={0}", id);
            return true;
        }

        public bool Delete()
        {
            Product product = null;

            Product.BeforeDeleteProduct(product);

            //delete

            Product.AfterDeleteProduct(product);
            return false;
        }

        public ProductTasks()
        {

        }

        #region ITaskReturnUrl Members
        private string m_returnUrl = "";
        public string ReturnUrl
        {
            get { return m_returnUrl; }
        }

        #endregion

        public int InsertProduct()
        {
            Product product = new Product();
            int manufacturerId = 0;
            int.TryParse(TypeID.ToString(), out manufacturerId);

            SiteConfig.Load();

            int successOrNot = new SubSonic.Insert().Into(Product.Schema).Values(
                    Alias
                   ,System.Guid.NewGuid().ToString()    
                   ,Alias
                   ,Alias
                   ,manufacturerId
                   ,DBNull.Value
                   ,1
                   ,1
                   ,1
                   ,1
                   ,1
                   ,DBNull.Value
                   ,0
                   ,0
                   ,0
                   ,SiteConfig.CurrencyCode.ToString()
                   ,"each"
                   ,DBNull.Value
                   ,0
                   ,0
                   ,0
                   ,"each"
                   ,0
                   ,99
                   ,4
                   ,1
                   ,DBNull.Value
                   ,SubSonic.Utilities.Utility.FormatDate(DateTime.Now, true)
                   ,"Commerce For Umbraco"
                   , SubSonic.Utilities.Utility.FormatDate(DateTime.Now, true)
                   , "Commerce For Umbraco")
                    .Execute();

            if (successOrNot == 1)
            {
                product = new Product(Alias);
                Product.AfterNewProduct(product);
            }

            
            return product.ProductID;

        }
    }

    public class ManufacturerTasks : umbraco.interfaces.ITaskReturnUrl
    {
        private string _alias;
        private int _parentID;
        private int _typeID;
        private int _userID;

        public int UserId
        {
            set { _userID = value; }
        }

        public int TypeID
        {
            set { _typeID = value; }
            get { return _typeID; }
        }

        public int ParentID
        {
            set { _parentID = value; }
            get { return _parentID; }
        }

        public string Alias
        {
            get
            {
                return _alias;
            }
            set
            {
                _alias = value;
            }
        }

        public bool Save()
        {
            Manufacturer thisManufacturer = new Manufacturer();
            thisManufacturer.ManufacturerX = Alias;
            thisManufacturer.Save();

            m_returnUrl = string.Format("commerce/editManufacturer.aspx?m={0}", thisManufacturer.ManufacturerID);

            return true;
        }

        public bool Delete()
        {

            return false;
        }

        public ManufacturerTasks()
        {

        }

        #region ITaskReturnUrl Members
        private string m_returnUrl = "";
        public string ReturnUrl
        {
            get { return m_returnUrl; }
        }

        #endregion

    }

    public class CouponTasks : umbraco.interfaces.ITaskReturnUrl
    {
        private string _alias;
        private int _parentID;
        private int _typeID;
        private int _userID;

        public int UserId
        {
            set { _userID = value; }
        }

        public int TypeID
        {
            set { _typeID = value; }
            get { return _typeID; }
        }

        public int ParentID
        {
            set { _parentID = value; }
            get { return _parentID; }
        }

        public string Alias
        {
            get
            {
                return _alias;
            }
            set
            {
                _alias = value;
            }
        }

        public bool Save()
        {
            object[] args = new object[2];
            args[0] = Alias;
            args[1] = Commerce.Promotions.CouponType.GetCouponType(TypeID);

            object tempcoup = Activator.CreateInstance(Commerce.Promotions.CouponType.GetCouponType(TypeID).CouponClassType, args);

            Commerce.Promotions.Coupon newCoupon = (Commerce.Promotions.Coupon)tempcoup;

           
            newCoupon.IsSingleUse = false;
            newCoupon.ExpirationDate = DateTime.UtcNow.AddYears(1);
            newCoupon.UserId = new Guid();
            newCoupon.SaveCoupon();
      

            m_returnUrl = string.Format("commerce/editCoupon.aspx?c={0}", Alias);

            return true;
        }

        public bool Delete()
        {
           // Coupon thisCoupon = new Coupon("CouponCode",
            return false;
        }

        public CouponTasks()
        {

        }

        #region ITaskReturnUrl Members
        private string m_returnUrl = "";
        public string ReturnUrl
        {
            get { return m_returnUrl; }
        }

        #endregion

    } 
}
